package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.AdminDeleteDomain;
import ch.fhnw.qualiscan.application.modals.AdminEditDomain;
import ch.fhnw.qualiscan.application.modals.AdminNewDomain;
import ch.fhnw.qualiscan.application.pages.Admin;
import ch.fhnw.qualiscan.core.db.helpers.adminDomainTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Language;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

/**
 * Edit domains.
 *
 */
@SuppressWarnings("serial")
public class AdminTab1 extends VerticalLayout {
	
	public Admin admin;
	private Table domainTable;
	public BeanItemContainer<adminDomainTableArgs> tableContainer;
	private Language lang;
	
	public AdminTab1(Admin admin) {
		this.admin = admin;
		lang = admin.userSession.getLanguage();

		buildLayout();
	}
	
	public void buildLayout() {
		removeAllComponents();
		addComponent(addDomainForm());
		
		VerticalLayout tableWrap = new VerticalLayout();
		tableWrap.addComponent(buildTable());
		tableWrap.setMargin(true);
		addComponent(tableWrap);
	}
	
	public Table buildTable() {
		tableContainer = new BeanItemContainer<adminDomainTableArgs>(adminDomainTableArgs.class);
		
		List<Domain> domainList = admin.adminModel.getDomains();
		for(int i = 0; i < domainList.size(); i++) {
			adminDomainTableArgs adta = new adminDomainTableArgs();
			adta.setDomain(domainList.get(i));
			adta.setNumberQuestions(domainList.get(i).getQuestions().size());
			adta.setTitleEng(admin.adminModel.getDomainText(domainList.get(i), lang).getText());
			
			tableContainer.addBean(adta);
		}
		
		domainTable = new Table();
		domainTable.setWidth("100%");
		domainTable.setPageLength(0);
		domainTable.setSelectable(true);
		
		domainTable.addGeneratedColumn("domainEditButton", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
			
 				Button editButton = new Button(TM.get("action.edit"), new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						adminDomainTableArgs adta = (adminDomainTableArgs) event.getButton().getData();
						AdminTab1 caller = (AdminTab1) event.getButton().getParent().getParent().getParent();
						admin.openModal(new AdminEditDomain(adta, caller));
					}
				});
 				editButton.setData(itemId);
				editButton.setIcon(Config.ICON_EDIT);
				return editButton;
			}
		});
		
		domainTable.addGeneratedColumn("domainDeleteButton", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
				
 				Button deleteButton = new Button(TM.get("action.delete"), new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						adminDomainTableArgs adta = (adminDomainTableArgs) event.getButton().getData();
						AdminTab1 caller = (AdminTab1) event.getButton().getParent().getParent().getParent();
						admin.openModal(new AdminDeleteDomain(adta, caller));
					}
				});
 				deleteButton.setData(itemId);
				deleteButton.setIcon(Config.ICON_CANCEL);
				
				return deleteButton;
			}
		});
		
		domainTable.setContainerDataSource(tableContainer);
		
		domainTable.setColumnHeader("titleEng", TM.get("admin.domaintable.engtitle"));
		domainTable.setColumnHeader("numberQuestions", TM.get("admin.domaintable.numberquestions"));
		domainTable.setColumnHeader("domainEditButton", TM.get("action.edit"));
		domainTable.setColumnHeader("domainDeleteButton", TM.get("action.delete"));
		
		domainTable.setVisibleColumns(new Object[]{"titleEng", "numberQuestions", "domainEditButton", "domainDeleteButton"});
		
		return domainTable;
	}
	
	public HorizontalLayout addDomainForm() {
		HorizontalLayout addDomainForm = new HorizontalLayout();
		addDomainForm.setMargin(true, true, false, true);
		
		Button addButton =  new Button(TM.get("action.add"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminTab1 caller = (AdminTab1) event.getButton().getParent().getParent();
				admin.openModal(new AdminNewDomain(tableContainer, caller));
			}
		});
		addButton.setIcon(Config.ICON_ADD);
		addDomainForm.addComponent(addButton);
		
		return addDomainForm;
	}
}
